package com.colin.app.fragment.example;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.colin.app.R;
import com.colin.app.adapter.ExampleFragmentRecyclerViewAdapter;
import com.colin.app.app.AppActivity;
import com.colin.app.app.AppFragment;
import com.colin.library.help.LogHelp;
import com.colin.library.other.Constants;
import com.colin.library.recycler.RecyclerViewItemDecoration;

/**
 * A simple {@link Fragment} subclass.
 */
public class ExampleFragment extends AppFragment implements ExampleContract.ExampleView {
    private ExampleContract.ExamplePresenter mExamplePresenter = null;
    private static final String FRAGMENT_ID = "fragment_id";
    private int fragment_id;
    private int layoutType = Constants.FRAGMENT_RECYCLER_VIEW_TYPE_LIST;
    private RecyclerView recycler_example_show;
    private ExampleFragmentRecyclerViewAdapter adapter = null;

    public void setLayoutType(int layoutType) {
        this.layoutType = layoutType;
        adapterNewLayout();
    }


    public static ExampleFragment newInstance(AppActivity mAppActivity, int fragment_id) {
        activity = mAppActivity;
        ExampleFragment fragment = new ExampleFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(FRAGMENT_ID, fragment_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        fragment_id = bundle.getInt(FRAGMENT_ID);
    }

    @Override
    public void onDestroyView() {
        if (null != mExamplePresenter) {
            mExamplePresenter.destroy();
            mExamplePresenter = null;
        }
        super.onDestroyView();
    }

    @Override
    public void initPresenter() {
        mExamplePresenter = new ExamplePresenterImpl((AppActivity) activity, this, fragment_id);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.fragment_example;
    }

    @Override
    public void initView() {
        this.recycler_example_show = (RecyclerView) this.getView().findViewById(R.id.recycler_example_show);
        initRecyclerView();
    }

    private void initRecyclerView() {
        if (adapter == null) {
            adapter = new ExampleFragmentRecyclerViewAdapter(activity, mExamplePresenter.getItemList());
        }
        adapterNewLayout();
    }

    private void adapterNewLayout() {
        try {
            recycler_example_show.setAdapter(null);
            RecyclerView.LayoutManager layoutManager = null;
            if (layoutType == Constants.FRAGMENT_RECYCLER_VIEW_TYPE_GRID) {
                final int grid_columns = activity.getResources().getInteger(R.integer.grid_columns);
                layoutManager = new GridLayoutManager(activity, grid_columns);
            } else if (layoutType == Constants.FRAGMENT_RECYCLER_VIEW_TYPE_STAGGERED) {
                final int grid_columns = activity.getResources().getInteger(R.integer.grid_columns);
                layoutManager = new StaggeredGridLayoutManager(grid_columns, OrientationHelper.VERTICAL);
            } else {
                layoutManager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
            }
            this.recycler_example_show.setLayoutManager(layoutManager);
            //设置adapter
            recycler_example_show.setAdapter(adapter);
            //设置添加,移除item的动画,DefaultItemAnimator为默认的
            recycler_example_show.setItemAnimator(new DefaultItemAnimator());
            //添加分割线
            recycler_example_show.addItemDecoration(new RecyclerViewItemDecoration(activity, RecyclerViewItemDecoration.VERTICAL_LIST));
        }catch (Exception e){
            LogHelp.e(e.getMessage());
        }

    }

    @Override
    public void initListener() {
        adapter.setOnItemClickListener(mListener, fragment_id);
    }
}
